import {
  CreateOrUpdateDataSourceParam,
  DataSourceListItem,
  DataSourcePageParam,
  DBVersionItem,
  DeleteParam,
  DetailParams,
  DiskTypeItem,
  RdsDiskTypeItem,
  RdsSizeCodeItem,
  SizeCodeParam,
  TestParam,
  UpdateParams,
} from './model/sourceModel';
import { deployHttp } from '/@/utils/http/axios';

enum Api {
  GET_DATA_SOURCE_LIST = '/v1/datasource/getList',
  UPDATE_DATA_SOURCE = '/v1/datasource/update',
  CREATE_DATA_SOURCE = '/v1/datasource/create',
  DELETE_DATA_SOURCE = '/v1/datasource/delete',
  GET_DATA_SOURCE_DETAIL = '/v1/datasource/detail',
  TEST_DATA_SOURCE = '/v1/datasource/testDatasourceInfo',
  GET_DATABASE_TYPE = '/v1/datasource/getDbType',
  UPDATE_RDS = '/v1/datasource/updateRdsInstanceInfo',
  UPDATE_GUASSDB = '/v1/datasource/updateGaussDBInstanceInfo',
  GET_RDS = '/v1/datasource/getRdsInstanceInfo',
  GET_GUASSDB = '/v1/datasource/getGaussDBInstanceInfo',
  GET_GDB_SIZE_CODE = '/v1/datasource/getGaussDbSizeCode',
  GET_RDS_SIZE_CODE = '/v1/datasource/getRdsInstanceSizeCode',
  GET_DISK_TYPE = '/v1/datasource/getDiskType',
  GET_RDS_VERSION = '/v1/datasource/getRdsDBVersion',
  GET_GUASS_VERSION = '/v1/datasource/getGaussDbVersion',
  GET_RDS_DISK_TYPE = '/v1/datasource/getRdsDbDiskType',
}

export const getSourceList = async (params: DataSourcePageParam) => {
  const result = await deployHttp.get<DataSourceListItem[]>({
    url: Api.GET_DATA_SOURCE_LIST,
    params,
  });

  result.items?.forEach((res) => {
    switch (res.isUsed) {
      case false:
        res.isUsed = '否';
        break;
      case true:
        res.isUsed = '是';
        break;
    }
  });
  return result;
};

export const updateData = (params: CreateOrUpdateDataSourceParam) => {
  return deployHttp.post<CreateOrUpdateDataSourceParam>({
    url: Api.UPDATE_DATA_SOURCE,
    params,
  });
};

export const dataSourceDetail = (params: DetailParams) => {
  return deployHttp.get<string>({ url: Api.GET_DATA_SOURCE_DETAIL, params });
};

export const createDataSource = (params: CreateOrUpdateDataSourceParam) => {
  return deployHttp.post<string>({ url: Api.CREATE_DATA_SOURCE, params });
};

export const deleteDataSource = (params: DeleteParam) => {
  return deployHttp.get<string>({ url: Api.DELETE_DATA_SOURCE, params });
};

export const testDataSource = (params: TestParam) => {
  return deployHttp.get<string>({ url: Api.TEST_DATA_SOURCE, params });
};

export const getDbType = () => {
  return deployHttp.get<string>({ url: Api.GET_DATABASE_TYPE });
};
export const getRdsInstanceInfo = () => {
  return deployHttp.get<UpdateParams>({ url: Api.GET_RDS });
};
export const getGaussDBInstanceInfo = () => {
  return deployHttp.get<UpdateParams>({ url: Api.GET_GUASSDB });
};
export const updateRdsInstanceInfo = (params: UpdateParams) => {
  return deployHttp.post<string>({ url: Api.UPDATE_RDS, params });
};
export const updateGaussDbInstanceInfo = (params: UpdateParams) => {
  return deployHttp.post<string>({ url: Api.UPDATE_GUASSDB, params });
};
export const getGaussDBSizeCode = async (params: SizeCodeParam) => {
  const res = await deployHttp.get<RdsSizeCodeItem[]>({ url: Api.GET_GDB_SIZE_CODE, params });
  const result = res.map((item) => {
    return {
      label: item.ram + 'G内存，' + item.vcpus + 'vcpu',
      value: item.spec_code,
    };
  });
  return result;
};
export const getRdsInstanceSizeCode = async (params: SizeCodeParam) => {
  const res = await deployHttp.get<RdsSizeCodeItem[]>({ url: Api.GET_RDS_SIZE_CODE, params });
  const result = res.map((item) => {
    return {
      label: item.ram + 'G内存，' + item.vcpus + 'vcpu',
      value: item.spec_code,
    };
  });
  return result;
};
export const getDiskType = async () => {
  const res = await deployHttp.get<DiskTypeItem[]>({ url: Api.GET_DISK_TYPE });
  const result = res.map((item) => {
    return {
      label: item.description,
      value: item.type,
    };
  });
  return result;
};
export const getRdsDBVersion = async (param: string) => {
  const params = { databaseName: param };
  const res = await deployHttp.get<DBVersionItem[]>({ url: Api.GET_RDS_VERSION, params });
  const result = res.map((item) => {
    return {
      label: item.name,
      value: item.name,
    };
  });
  return result;
};
export const getRdsDiskType = async (params: SizeCodeParam) => {
  const res = await deployHttp.get<RdsDiskTypeItem[]>({ url: Api.GET_RDS_DISK_TYPE, params });
  const result = res.map((item) => {
    return {
      label: item.name,
      value: item.name,
    };
  });
  return result;
};
export const getGuassDbVersion = async (param: string) => {
  const params = { databaseName: param };
  const res = await deployHttp.get<DBVersionItem[]>({ url: Api.GET_GUASS_VERSION, params });
  const result = res.map((item) => {
    return {
      label: item.name,
      value: item.name,
    };
  });
  return result;
};
